function f1(arr){
    const temp = arr.reduce((a, b)=>{
        a[b.id] = b;
        return a
    }, {})

    const tree = {}

    for (const i in temp) {
       const pid = temp[i]['parentId'];

       if (pid) {
           if (!temp[pid].children) {
            temp[pid].children = []
           }

           temp[pid].children.push(temp[i])
       } else {
        tree[temp[i]['id']] = temp[i];
       }
    }

    return tree
}

const q = f1([
   
    {
        id:111,
        parentId:11,
        name:'111'
    },
    {
        id:21,
        parentId:2,
        name:'21'
    },
    {
        id:22,
        parentId:2,
        name:'22'
    },
    {
        id:221,
        parentId:22,
        name:'221'
    },
    {
        id:2211,
        parentId:221,
        name:'2211'
    },
    {
        id:22111,
        parentId:2211,
        name:'22111'
    },
    {
        id:1,
        name:'1'
    },
    {
        id:2,
        name:'2'
    },
    {
        id:11,
        parentId:1,
        name:'11'
    },
])

console.log(JSON.stringify(q))